//
//  KSKABarChart.m
//  DashboardHD
//
//  Created by 楊亜玲 on 17/3/28.
//  Copyright © 2017年 Totyu. All rights reserved.
//
#define BarX(stri) [(stri) floatValue] * (self.width / TotalLength)
#define TotalLength 1440

#import "KSKABarChart.h"

@interface KSKABarChart ()

@property(nonatomic, strong) NSArray *dataSourceArray;

@end

@implementation KSKABarChart

-(instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.2];
        
        self.dataSourceArray=[NSArray arrayWithArray:[[dataSource reverseObjectEnumerator] allObjects]];
        //self.dataSourceArray =dataSource;
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    //获得处理的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置线条粗细宽度
    CGContextSetLineWidth(context, self.frame.size.width / TotalLength * 2);
    for (int i = 0; i < self.dataSourceArray.count; i++) {
        NSDictionary *tmpDict = [self.dataSourceArray objectAtIndex:i];
        // 设置线条颜色
        [[UIColor colorWithHex:tmpDict[@"actioncolor"]] setStroke];
        NSArray *tmpArray = tmpDict[@"data"];
        [tmpArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            CGContextMoveToPoint(context, BarX(obj), self.height);
            CGContextAddLineToPoint(context, BarX(obj), 0);
        }];
        CGContextDrawPath(context, kCGPathStroke);
    }
    
    // 绘制辅助线
    CGContextSetLineWidth(context, 0.3);
    [[UIColor blackColor] setStroke];
    CGFloat arr[] = { 2, 1};
    CGContextSetLineDash(context, 0, arr, 2);
    for (int i = 0; i <= 24; i++) {
        if (i > 0 && i < 24) {
            CGContextMoveToPoint(context, self.width / 24 * i, self.height);
            CGContextAddLineToPoint(context, self.width / 24 * i, 0);
        }
    }
    CGContextDrawPath(context, kCGPathStroke);
    
}



@end
